/*
 * vendorClient.cpp
 *
 *  Created on: Apr 8, 2012
 *      Author: nikhil
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include<iostream>
#include<fstream>
#include<string>
#include<exception>
#include<vector>
#include "FileUtilities.h"

using namespace std;
using namespace utilities;

#define	BUFSIZE 256
const int CMD_LENGTH = 10;
const char* CMD_NEWDATA = "cmd_nwdata";
const char* ACK_NEWDATA = "ack_nwdata";
const char* CMD_REDY = "cmd__ready";

int	connectTCP(const char *host, const char *service);
int main(int argc, char *argv[])
{
	char	*host = "localhost";	/* host to use if none supplied	*/
	char	*service = "5757";	/* default service name		*/
	char *strVendorID="43";
	char *strFileName="/home/nikhil/zearth_qcif_cif_4cif.svc";
	/* 32-bit integer to hold time	*/
	int	s, n;			/* socket descriptor, read count*/

	char *buff=(char *)malloc(BUFSIZE);

	switch (argc) {
		case 1:
			host = "localhost";
			break;

		case 5:
			strVendorID=argv[4];
			strFileName=argv[3];
		case 3:
			service = argv[2];
			// FALL THROUGH
		case 2:
			host = argv[1];
			break;
		default:
			cerr<<"usage: UDPtime [host [port]]\n";
			exit(1);
	}

	s = connectTCP(host, service);
	cerr<<"connevted\n";
	//write command
	//(void) write(s, cmd, strlen(cmd));
	(void) send(s, CMD_NEWDATA,CMD_LENGTH,0);


	char  *cmdACK=(char *) malloc (CMD_LENGTH);

	//while( (len = read(s,cmdACK,BUFSIZE)) < 0);
	//int len=read(s,cmdACK,BUFSIZE);
		int len=recv(s,cmdACK,BUFSIZE,0);

			cmdACK[len] = '\0';
	cerr<<"Command ack received is :"<<cmdACK;

	if(!strcmp(ACK_NEWDATA,cmdACK))
	{
		cerr<<"command validated so getting ready to send filename"<<endl;

	}
	else
	{
		fprintf(stderr,"Command NEWDATAnot validated\n");
		exit(1);
	}



	cerr<<"Sent file name and vendor id appended to Main server"<<endl;


	string strFileNameVendorID;

	strFileNameVendorID.append(strFileName);
	strFileNameVendorID.append(",");
	strFileNameVendorID.append(strVendorID);


	(void) send(s, strFileNameVendorID.c_str(), strlen(strFileNameVendorID.c_str()),0);


   /*
	char *recvCmdRedy=(char *)malloc(CMD_LENGTH);
	int len2=-1;
	//while( (len2 = read(s,strContentReq,strlen(strContentReq))) < 0);
	//int len2=read(s,strContentReq,strlen(strContentReq));
	len2=recv(s,recvCmdRedy,CMD_LENGTH,0);


	recvCmdRedy[len2] = '\0';

	cerr<<"command REDY received is :"<<recvCmdRedy<<endl;


	if(!strcmp(recvCmdRedy,CMD_REDY))
	{
		cerr<<"coomand ready received successfully"<<endl;
		cerr<<"Will now send the vendor ID"<<endl;

	}
	else
	{
		cerr<<"command ready NOT received properly so terminating application\n"<<endl;
		exit(1);
	}

		cerr<<"Sent vendor Id to Main server"<<endl;
		(void) send(s, strVendorID, strlen(strVendorID),0);

		//May add dummy recv to add delay to		 */
}
